package csc.fresher.dao.impl;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import csc.fresher.dao.InternetAccountDAO;
import csc.fresher.entity.InternetAccount;

@Repository
public class InternetAccountDAOImpl implements InternetAccountDAO, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private DataSource dataSource;
	private static final Logger logger = Logger
			.getLogger(InternetAccountDAOImpl.class);

	@Override
	public InternetAccount getInternetAccountByUsername(String username) {
		String query = "SELECT * FROM `finalproject`.`internetaccount` where `username`=?";
		InternetAccount internetAccount = null;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet rs = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setString(1, username);
			rs = preparedStatement.executeQuery();
			internetAccount = getInternetAccountResultSet(rs);

		} catch (SQLException e) {
			logger.error(e);
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
				if (rs != null) {
					rs.close();
				}
			} catch (SQLException e) {
				logger.error(e);
				e.printStackTrace();
			}
		}
		return internetAccount;
	}

	private InternetAccount getInternetAccountResultSet(ResultSet rs)
			throws SQLException {
		InternetAccount internetAccount = new InternetAccount();
		if (rs.next()) {
			internetAccount.setCustomerID(rs.getLong("customerid"));
			internetAccount.setUsername(rs.getString("username"));
			internetAccount.setPassword(rs.getString("password"));
			internetAccount.setLock(rs.getBoolean("isLock"));
		}
		return internetAccount;
	}

	@Override
	public void createInternetAccount(InternetAccount internetAcc) {
		String query = "INSERT INTO `finalproject`.`internetaccount` (`customerID`, `username`,`password`,`isLock`)"
				+ "VALUE(?,?,?,?)";
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setLong(1, internetAcc.getCustomerID());
			preparedStatement.setString(2, internetAcc.getUsername());
			preparedStatement.setString(3, internetAcc.getPassword());
			preparedStatement.setBoolean(4, internetAcc.isLock());
			preparedStatement.execute();
		} catch (SQLException ex) {
			logger.error(ex);
			ex.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException ex) {
				logger.error(ex);
				ex.printStackTrace();
			}
		}
	}

	@Override
	public void updateInternetAccount(InternetAccount internetAcc) {
		String query = "UPDATE `finalproject`.`internetaccount` SET `password`=?,`isLock`=? WHERE `customerid`=? ";
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setString(1, internetAcc.getPassword());
			preparedStatement.setBoolean(2, internetAcc.isLock());
			preparedStatement.setLong(3, internetAcc.getCustomerID());
			preparedStatement.execute();
		} catch (SQLException e) {
			logger.error(e);
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				logger.error(e);
				e.printStackTrace();
			}
		}
	}

	@Override
	public void deleteInternetAccount(InternetAccount internetAcc) {
		// TODO Auto-generated method stub

	}

}
